package com.dx.mes.web.common.service.impl;

import java.io.IOException;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.dx.mes.common.core.base.service.ICIMBaseInterface;
import com.dx.mes.web.common.service.ISendMessageService;
@Service
public class SendMessageServiceImpl implements ISendMessageService {
	private Logger logger = Logger.getLogger(SendMessageServiceImpl.class);
	private static Properties props;
	@Resource
	private ApplicationContext context;

    @PostConstruct
    public void init(){
        try {
            props = new Properties();
            props.load(SendMessageServiceImpl.class.getResourceAsStream("/triger.properties"));
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.toString());
        }
    }

	public String sendMesage(String trxId, String evt_no, String inTrx) {
		/**
		 * TODO:此处需要找trx_id和springServiceBean的对应关系 通过properties配置档
		 */
		String springBeanName = getBeanNameByTrxID(trxId);
		try {
			ICIMBaseInterface icimBaseinterFace = (ICIMBaseInterface) context.getBean(springBeanName);
			String mesage = icimBaseinterFace.subMainProc(evt_no,inTrx);
			return mesage;
		} catch (Exception e) {
			return null;
			}
	}

	private String getBeanNameByTrxID(String trx_id) {
		/**
		 * TODO:根据trx_id在配置档properties文件中找到service的名字
		 */
//		props = new Properties();
//		try {
//			props.load(SendMessageServiceImpl.class.getResourceAsStream("/triger.properties"));
//		} catch (IOException e) {
//			logger.error("Read trigger configuration error");
//			e.printStackTrace();
//			logger.error(e.toString());
//		}
		String beanName = props.getProperty(trx_id);
		return beanName;
	}
}
